package com.nl.lib_project.view.textview

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.nl.lib_core.R
import com.nl.lib_core.tool.extends.dp2px

/** 作者:LiBW 创建日期:2022/2/22 描述:TextView可拓展一个Drawable 设置方向和大小 */
class TextViewWithDrawable(context: Context, attributeSet: AttributeSet? = null) :
    AppCompatTextView(context, attributeSet) {

  init {
    val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.TextViewWithDrawable)
    val width = typedArray.getFloat(R.styleable.TextViewWithDrawable_drawable_weight, 0f)
    val height = typedArray.getFloat(R.styleable.TextViewWithDrawable_drawable_height, 0f)
    // 图片资源
    val drawable = typedArray.getDrawable(R.styleable.TextViewWithDrawable_drawable_src)
    // 图片方向
    // start:0 end:1 top:2 bottom:3
    val location = typedArray.getInt(R.styleable.TextViewWithDrawable_drawable_location, 0)
    if (width != 0f || height != 0f) {
      drawable?.setBounds(0, 0, context.dp2px(width), context.dp2px(height))
    }
    when (location) {
      0 -> setCompoundDrawables(drawable, null, null, null)
      1 -> setCompoundDrawables(null, null, drawable, null)
      2 -> setCompoundDrawables(null, drawable, null, null)
      3 -> setCompoundDrawables(null, null, null, drawable)
    }
    typedArray.recycle()
  }
}
